﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.ProgressBar;
using YArchitech.Revit.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public class RequestHandler : IExternalEventHandler
	{
		public Request Request
		{
			get
			{
				return this.m_request;
			}
		}

		public string GetName()
		{
			return "CableLaying External Event";
		}

		public void Execute(UIApplication uiapp)
		{
			UIDocument activeUIDocument = uiapp.ActiveUIDocument;
			Document document = activeUIDocument.ActiveView.Document;
			RequestId requestId = this.Request.Take();
			switch (requestId)
			{
			case RequestId.CableLaying:
			{
				if (Application.thisApp.m_lstData == null || Application.thisApp.m_lstData.Count <= 0)
				{
					return;
				}
                HYCodeBase.WinForm.WindowHandle owner = new HYCodeBase.WinForm.WindowHandle(Application.thisApp.ThisMainForm.Handle);
				ProgressBarForm progressBarForm = new ProgressBarForm(Application.thisApp.m_lstData.Count, ProgressBarStyle.Blocks, true);
				progressBarForm.Show(owner);
				for (int i = 0; i < Application.thisApp.m_lstData.Count; i++)
				{
					CableListData cableListData = Application.thisApp.m_lstData[i];
					cableListData = new AutomaticLaying(document, cableListData).Laying();
					cableListData.Length = ((cableListData.Length.Trim() == "0") ? string.Empty : cableListData.Length);
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						break;
					}
				}
				Application.thisApp.ThisMainForm.UpdateDataAfterAutoLaying();
				return;
			}
			case RequestId.LookUpLayPath:
			{
				if (Application.thisApp.m_lstData == null || Application.thisApp.m_lstData.Count <= 0)
				{
					return;
				}
				Application.thisApp.ThisMainForm.WindowState = FormWindowState.Minimized;
				CableListData cableListData2 = Application.thisApp.m_lstData[0];
				new CableTrayLookUp(activeUIDocument, cableListData2.LayPathIDs).LookUp();
				return;
			}
			case RequestId.RemoveLayCable:
				if (Application.thisApp.m_lstData == null || Application.thisApp.m_lstData.Count <= 0)
				{
					return;
				}
				CableLayRevitCommon.CableTraySchemaDataRemove(document, Application.thisApp.m_lstData);
				Application.thisApp.ThisMainForm.UpdateDataAfterRemove();
				return;
			case RequestId.RemoveAllLayCable:
				CableLayRevitCommon.CableTraySchemaDataRemove(document);
				Application.thisApp.ThisMainForm.UpdateDataAfterRemoveAll();
				return;
			default:
				if (requestId != RequestId.Equipment_Assignment)
				{
					if (requestId != RequestId.Equipment_Layout)
					{
						return;
					}
					if (Application.thisApp.ThisELForm == null)
					{
						return;
					}
					if (new EquipmentList(activeUIDocument, Application.thisApp.ThisELForm).Layout())
					{
						Application.thisApp.ThisELForm.RefreshSelectedGridRowState();
					}
				}
				else
				{
					if (Application.thisApp.ThisELForm == null)
					{
						return;
					}
					if (new EquipmentList(activeUIDocument, Application.thisApp.ThisELForm).Assignment())
					{
						Application.thisApp.ThisELForm.RefreshSelectedGridRowState();
						return;
					}
				}
				return;
			}
		}

		private Request m_request = new Request();
	}
}
